package com.tau.egonetwork.profiles;

import java.io.Serializable;

public abstract class Profile<T extends Comparable<T>> implements IProfile<T>, Serializable, Cloneable {

	private static final long serialVersionUID = -8416252077939517841L;

	private T _id;
	private String _name;
	private Gender _gender;
	private String _location;
	private String _workLocation;
	
	private FriendsList<T> _friendslist = null;
	
	public Profile(T id, String name) {
		this._id = id;
		this._name = name;
		this._gender = null;
		this._location = null;
		this._workLocation  = null;
	}
	
	@Override
	public T getID() {
		return _id;
	}

	@Override
	public String getName() {
		return _name;
	}
	
	@Override
	public Gender getGender() {
		return _gender;
	}
	
	@Override
	public String getLocation() {
		return _location;
	}
	
	@Override
	public String getWorkLocation() {
		return _workLocation;
	}
	
	@Override
	public void setName(String name) {
		this._name = name;
	}

	
	@Override
	public void setGender(Gender gender) {
		_gender = gender;
	}
	
	@Override
	public void setGender(String gender) {
		_gender = gender.equals("male") ? Gender.male :
			(gender.equals("female") ? Gender.female : Gender.undefined);
	}
	
	@Override
	public void setLocation(String location) {
		_location = location;
	}
	
	@Override
	public void setWorkLocation(String work) {
		_workLocation = work;
	}
	
	@Override
	public FriendsList<T> createFriendsList() {
		_friendslist = new FriendsList<T>();
		return _friendslist;
	}
	
	@Override
	public FriendsList<T> getFriendsList() {
		return _friendslist;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public Object clone() {
		try {
			Profile<T> clonedObject = (Profile<T>)super.clone();
			clonedObject._friendslist = null;
			return clonedObject;
		} catch (CloneNotSupportedException e) {
			//object is cloneable (!), not problem here, but we have to
			//do something because super throws, so just do this:
			System.out.println("CloneNotSupportedException");
			return null;
		}
	}
	
	@Override
	public String toString() {
		return _id + ":" + _name + ", " + (_gender == null ? "" : _gender)
				+ (_location == null ? "" : ", " + _location)
				+ (_workLocation == null ? "" : ", works at: " + _workLocation)
				+ ", # of friends: " + (_friendslist != null ? _friendslist.size() : 0);
	}
	
	/**
	 * Copies shallow data (only fields, not friends list)
	 * */
	protected void copyFrom(Profile<T> toCopy) {
		_id = toCopy.getID();
		_name = toCopy.getName();
		_location = toCopy.getLocation();
		_workLocation = toCopy.getWorkLocation();
	}
	
}
